const models = require(__base + '/models');
const errcode = require(__util + "/errcode")
const ct = require(__util + "/catch"); // 路由错误捕获(不使用则不会捕获异常错误)

module.exports = ct(async (req, res) => {
  const {
    appIcon, appName, appType, appUrl, platform = '', seqNum = 1, gteVersion, lteVersion, appCode, chatDapp = false
  } = req.body

  if (!appIcon || !appName || !appType || !appUrl) {
    return res.json(errcode.APP_UNFULL)
  }

  if (!(/(^[1-9]\d*$)/.test(seqNum))) {
    return res.json(errcode.APP_SEQNUM_INVALID)
  }

  let appInfo = await models.ThirdPartyApp.create({
    appIcon: appIcon,
    appName: appName,
    appType: appType,
    appUrl: appUrl,
    platform: platform,
    seqNum: seqNum,
    gteVersion: gteVersion,
    lteVersion: lteVersion,
    appCode: appCode,
    chatDapp: chatDapp
  })

  // 获取数据内部模型类
  appInfo = appInfo.get({ plain: true })
  // 利用对象的解构，取出非createdAt的所有字段
  const { updatedAt, ...newAppInfo } = appInfo

  return res.json(
    errcode.SUCCESS(newAppInfo)
  )
})